<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use function Matrix\add;    //引入hash

class MessageRepository {

    /**
     * 新增
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function addSysMessage($param) {
        $addConfig = app('model_sys_messages')->create($param);

        if ($addConfig) return $addConfig;
        throw new BasicException(93045);
    }

    /**
     * 删除
     */
    public function delSysMessage($idArr = []) {
        $res = app('model_sys_messages')->whereIn("id", $idArr)->delete();

        if ($res) return $res;
        throw new BasicException(93044);
    }

    /**
     * 修改
     */
    public function saveSysMessage($where,$param) {
        if (isset($param['content'])) {
            $param['content'] = trim($param['content'], ';');
        }
        app('model_sys_messages')->where($where)->update($param);
    }

    /**
     * 发送
     */
    public function sendSystemMessage($sysId, $params) {

        $insertArr = [];
        if ($params['user_type'] ==1){
            $max_user_id = app('model_user')->where("deleted",0)->orderBy('user_id','desc')->first()->user_id;
            $insertArr[] = [
                "max_user_id" => $max_user_id,
                "user_type" => $params['user_type'],
                "user_ids" => "",
                "message_id" => $sysId,
                "add_time" => time(),
            ];
        }elseif ($params['user_type'] ==2){
            if (!empty($params['user_id_arr'])) {
                $insertArr[] = [
                    "max_user_id" => 0,
                    "user_type" => $params['user_type'],
                    "user_ids" => $params['user_codes'],
                    "message_id" => $sysId,
                    "add_time" => time(),
                ];
            }
        }elseif ($params['user_type'] ==3){
            $max_user_id = app('model_user_data')->where("sex",1)->orderBy('user_id','desc')->first()->user_id;
            $insertArr[] = [
                "max_user_id" => $max_user_id,
                "user_type" => $params['user_type'],
                "user_ids" => "",
                "message_id" => $sysId,
                "add_time" => time(),
            ];
        }elseif ($params['user_type'] ==4){
            $max_user_id = app('model_user_data')->where("sex",2)->orderBy('user_id','desc')->first()->user_id;
            $insertArr[] = [
                "max_user_id" => $max_user_id,
                "user_type" => $params['user_type'],
                "user_ids" => "",
                "message_id" => $sysId,
                "add_time" => time(),
            ];
        }elseif ($params['user_type'] ==5){//会长
            $max_user_id = app('model_sc_sociaty')->select('user_id')->where('delete_time', 0)->orderBy('user_id', 'desc')->first()->user_id;
            $insertArr[] = [
                "max_user_id" => $max_user_id,
                "user_type" => $params['user_type'],
                "user_ids" => "",
                "message_id" => $sysId,
                "add_time" => time(),
            ];
        }elseif ($params['user_type'] ==6){//厅主
            $max_user_id = app('model_sc_hall')->select('user_id')->where('delete_time', 0)->orderBy('user_id', 'desc')->first()->user_id;
            $insertArr[] = [
                "max_user_id" => $max_user_id,
                "user_type" => $params['user_type'],
                "user_ids" => "",
                "message_id" => $sysId,
                "add_time" => time(),
            ];
        }elseif ($params['user_type'] ==7){//公会成员
            $max_user_id = app('model_sc_sociaty_user')->select('user_id')->orderBy('user_id', 'desc')->first()->user_id;
            $insertArr[] = [
                "max_user_id" => $max_user_id,
                "user_type" => $params['user_type'],
                "user_ids" => "",
                "message_id" => $sysId,
                "add_time" => time(),
            ];
        }else{
            throw new BasicException(93047, "用户类型错误");
        }
        $sysInfo = app('model_sys_messages')->where('id',$sysId)->first();
        $logData = [
            'title'     => $sysInfo->title ?? '',
            'content'   => $sysInfo->content ?? '',
            'user_type' => $params['user_type'],
            'user_ids'  => $params['user_codes'] ?? 0,
            'admin_id'  => $params['admin_id'] ?? 0,
            'add_time'  => time()
        ];
        $logRes = app('model_system_message_log')->create($logData);
        if (!$logRes) throw new BasicException(93047);
        $saveConfig = DB::connection("mysql")->table(app('model_system_message')->getTable())->insert($insertArr);
        if ($saveConfig) return $saveConfig;
        throw new BasicException(93047);
    }

    /**
     * 管理员后台查询配置数据
     */
    public function getSysMessageQueryList(Array $params) {
        $whereArr = [];

        $whereFunc = function ($query) use ($params) {
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $result = app('model_sys_messages')->where($whereArr)->where($whereFunc)->orderBy('id','desc')->paginate($perPage, ['*'], '', $page);

        foreach ($result as $item) {
            $item->add_time = date("Y-m-d H:i:s", $item->add_time);
        }

        return $result;
    }

    /**
     * 消息记录发送日志
     */
    public function getSysMessageLogQueryList(Array $params) {
        $whereArr = [];

        if (!empty($params['send_start_time']) && !empty($params['send_end_time'])) {
            $whereArr[] = ['add_time','>=',$params['send_start_time']];
            $whereArr[] = ['add_time','<=',$params['send_end_time']];
        }

        if (!empty($params['user_code'])) {
            $whereArr[] = ['user_ids', 'like', "%".$params['user_code']."%"];
        }
        if (!empty($params['admin_id'])) {
            $whereArr[] = ['admin_id', '=', $params['admin_id']];
        }
        $whereFunc = function ($query) use ($params) {
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $result = app('model_system_message_log')
            ->where($whereArr)
            ->where($whereFunc)
            ->with('adminInfo')
            ->orderBy('id','desc')
            ->paginate($perPage, ['*'], '', $page);

        foreach ($result as $item) {
            $item->add_time = date("Y-m-d H:i:s", $item->add_time);
        }

        return $result;
    }
}
